#!/usr/bin/python2.7
#
# Copyright 2011 Google Inc. All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#     http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import django.template
from django.template import loader
from third_party.active_state.recipes.autoassign import autoassign

'''
Created on Dec 9, 2011

@author: ibrahimelfar
'''

class Context():
    @autoassign
    def __init__(self, name, language): pass

    def __repr__(self):
        return '''{self.__class__}(name='{self.name}', language='{self.language}')'''.format(self = self)

class Template:
    @autoassign
    def __init__(self, name, value, context): pass

    def __repr__(self):
        return '''{self.__class__}(name='{self.name}', value='{self.value}', context='{self.context}')'''.format(self = self)

def generate_configuration(language_modules):
    templates = {}
    for language in language_modules:
        templates[language] = []
        module = language_modules[language]
        for term in module.patterns:
            print 'term ' + term
            for pattern in module.patterns[term]:
                print '  pattern ' + str(pattern)
                for core_template in pattern.generate_templates():
                    print '    core template ' + str(core_template)
                    if module.eclipse_contexts and module.eclipse_contexts.has_key(term):
                        for context in module.eclipse_contexts[term]:
                            templates[context.language].append(Template(name = core_template.name,
                                                                        value = core_template.value,
                                                                        context = context.name))

    for language in language_modules:
        f = open('''c:\\users\\ibrahimelfar\\''' + language + '-eclipse.xml', 'w')
        print loader.render_to_string('eclipse_templates.xml', django.template.Context({'templates': templates[language]}))
        f.write(loader.render_to_string('eclipse_templates.xml', django.template.Context({'templates': templates[language]})))
        f.flush()
        f.close()
